<?php
namespace Kuoqi;

class Format{


    /**
     * @param $str
     * @return float|int
     * User kuoqi
     * Datetime 2022/3/8 11:23
     * Remark 字符串转byte字符串
     */
    static function str2byte($str){
        $str = strtoupper($str);
        if(!preg_match('/^([\d\.]+)([M|K])/',$str,$match)){
            return 0;
        }
        if($match[2]=='M'){
            $code = floatval($match[1]) * 1048576;
        }else if($match[2]=='K'){
            $code = floatval($match[1]) * 1024;
        }else{
            $code = $match[1];
        }
        return $code;
    }



	//路由转路径
	static function route2path($route){
		$arr = explode(RL,$route);
		$orr = array(ROOT);
		foreach($arr as $it){
			if($it){
				$orr[] = $it;
			}
		}
		$last = array_pop($orr);
		$orr[] = $last;
		$orr[] = $last.Config::get('USER_EXT');
		return implode(DS,$orr);
	}
	
	//常规字符串转换为路由模式，不自动匹配前前后后的那种
	static function toRoute($str){
		$str = str_replace("/",RL,$str);
		$str = str_replace("\\",RL,$str);
		$str = str_replace('.',RL,$str);
		return $str;
	}
	
	static function port2Param($format){
		$arr = explode('.',$format);
		return self::formatIptFunc($arr);
	}
	
	
	//转换，inSelf:是否包含自身字段
	static final function formatIptFunc($ipts,$inSelf = false){
		$opt = '';
		foreach($ipts as $v){
			$opt .= $opt?'["'.$v.'"]':($inSelf?'["'.$v.'"]':$v);
		}
		return $opt;
	}
	
	//获取对象名称
	static final function getClassname($class){
		return substr(strrchr(get_class($class),'\\'),1);
	}
	
	//获取对象命名空间
	static final function getClassns($class){
		$cn = get_class($class);
		return substr($cn,0,strripos($cn,'\\')+1);
	}
	
	//获取模块名称，根据模块完整信息[eq:AdminModel->Admin]
	static final function getModelName($model,$ext){
		return str_replace($ext,'',$model);	
	}
	
	//尝试获取一类get信息，[eg:getName->name]
	static final function invokeGet($ivk_str){
		$a = strpos($ivk_str,'get');
		if($a===false){
			return false;	
		}else{
			return strtolower(substr($ivk_str,3));
		}
	}
	
	//配置用户密码
	static final function getPassword($code){
		if(strlen($code)==32){
			return $code;
		}
		return md5($code . strlen($code));	
	}
	
	//获取文件名
	static final function getFilename($fn,$hasExt = true){
		$filename = basename($fn);
		if(!$hasExt){
			$end = strrpos($filename,'.');
			$filename = substr($filename,0,$end?(-$end):strlen($filename));
		}
		return $filename;
	}
}